<template>
    <div class="login">
        <vue-particles id="tsparticles" :options="line" />
        <div class="login_form">
            <el-form :model="form" :rules="rules" ref="loginForm" label-width="auto" style="max-width: 600px">
                <el-form-item label="用户名" prop="username">
                    <el-input type="text" placeholder="请输入用户名" suffix-icon="User" class="ipt" v-model="form.username" />
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input type="password" show-password placeholder="请输入密码" class="ipt" v-model="form.password" />
                </el-form-item>
                <el-form-item label="验证码" prop="code">
                    <el-input type="text" placeholder="请输入验证码" class="ipt code" v-model="form.code" />
                    <img class="code_img" :src="codeImg" alt="" @click="refreshCode">
                </el-form-item>
                <div class="login_btn flex_c" @click="handlerLogin">登&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;录
                </div>
            </el-form>
        </div>
    </div>
</template>

<script setup lang="ts">
import { line } from "./particles"
import { ElMessage } from "element-plus";
import type { FormInstance, FormRules } from "element-plus";
import { getCaptcha, loginByUsername } from "@/api/user/user"
import { Md5 } from 'ts-md5'
import { useUserStore } from "@/stores/modules/user";
import { useRouter } from "vue-router"
/**
 * 账号密码登录
 */
interface LoginForm {
    username: string,
    password: string | number,
    code: string,
    key: string
}
const form = reactive<LoginForm>({
    username: "shadmin",
    password: "jt@123456",
    code: "",
    key: ""
})
// 输入框校验
const loginForm = ref<FormInstance>()
const rules = reactive<FormRules<LoginForm>>({
    username: [
        { required: true, message: "请输入用户名", trigger: "blur" }
    ],
    password: [
        { required: true, message: "请输入密码", trigger: "blur" },
        { min: 1, message: "密码长度最少为6位", trigger: "blur" }
    ]
})
// 登录
const user = useUserStore()
const router = useRouter()
const handlerLogin = async () => {
    if (!loginForm.value) return
    let flag = ref(false)
    // 表单校验
    await loginForm.value.validate((valid, fields) => {
        if (valid) {
            flag.value = true
        } else {
            flag.value = false
        }
    })
    if (!flag.value) return
    // 定义MD5对象
    const md5: any = new Md5()
    md5.appendAsciiStr(form.password)
    const password = md5.end()
    let par = {
        tenantId: "000000", //租户ID
        type: "account", //账号类型
        grant_type: "captcha",
        scope: "all",
        username: form.username,
        password,
        code: form.code,
        key: form.key
    }
    loginByUsername(par).then((res: any) => {
        if (res.access_token) {
            user.setUserInfo(res)
            user.setToken(res.access_token)
            user.setRefeshToken(res.refresh_token)
            ElMessage.success("登录成功!")
            router.push("/home/home")
        } else {
            form.code = ""
            getCaptchaData()
        }
    }).catch(() => {
        form.code = ""
        getCaptchaData()
    })
}

/**
 * 验证码
 */
// 验证码图片 默认一个预加载的白色背景
const codeImg = ref<string>("")
// 获取验证码
const getCaptchaData = () => {
    getCaptcha().then((res: any) => {
        codeImg.value = res.image
        form.key = res.key
    })
}
// 更换验证码
const refreshCode = () => {
    getCaptchaData()
}

onMounted(() => {
    getCaptchaData()
})
</script>

<style lang="scss" scoped>
.login {
    width: 100%;
    height: 100vh;
    overflow: hidden;
}

.login_form {
    width: 400px;
    padding: 24px 42px;
    box-sizing: border-box;
    background-color: rgba(255, 255, 255, 0.2);
    /* 半透明背景 */
    backdrop-filter: blur(10px) saturate(180%);
    /* 背景模糊 */
    -webkit-backdrop-filter: blur(10px) saturate(180%);
    /* 兼容老版本 Safari */
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    position: relative;
    margin: 0 auto;
    margin-top: 300px;
}

.ipt {
    width: 220px;
    height: 30px;
    border: none;
}

.code {
    width: 120px;

}

.code_img {
    width: 85px;
    height: 30px;
    margin-left: 12px;
    cursor: pointer;
}


.login_btn {
    width: 200px;
    height: 30px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 3px;
    margin: 0 auto;
    color: rgba(255, 255, 255, 0.6);
    cursor: pointer;

    &:hover {
        background-color: rgba(255, 255, 255, 0.6);
        color: #333;
    }
}

:deep(.el-form-item__label-wrap) {
    align-items: center;
    font-style: italic;
}

:deep(.el-form-item__label) {
    font-size: 16px;
    color: rgba(255, 255, 255, 0.6);
}

:deep(.el-input__wrapper) {
    background-color: transparent;
    box-shadow: none;
    border-radius: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.2);
}

:deep(.el-input__inner) {
    color: #fff;
}
</style>